/**
 * 自定义设备
 * */

var formList = [];


//参数列表
function getPasList(v){
    $.ajax({
        type: apis.devicePasIndex.method,
        url: apis.devicePasIndex.url,
        data: { type: v },
        success: function (res) {
            console.log(res);
            var tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                tbody.html('<tr class="text-c"><td colspan="10">没有记录</td></tr>');
                return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].type_name + '</td>';
                h += '<td>' + d[i].title + '</td>';
                h += '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="pas_edit(\'编辑\',\'dev-pas-edit.html\', ' + d[i].id + ')">';
                h += '修改';
                h += '<i class="Hui-iconfont">&#xe6df;</i>';
                h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
                h += '</a>';
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="pas_del(' + d[i].id + ');" style="color: red;">';
                h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
                h += '</a>';
                h += '</td>';
                h += '</td>';
                h += '</tr>';
            }
            tbody.html(h);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//编辑
function pas_edit(title,url,id,w,h){
    url += '?id=' + id;
    url += '&title=' + escape($('#title' + id).val());
    layer_show(title,url,w,300);
}


//更新参数
function pasModify(){
    var formData = {
        id: id,
        type: $('#type').val(),
        title: $('#title').val(),
    };
    if (!id && !formData.type) {
        msgPop({msg: '类型必须的'}); return
    }
    if (!formData.title) {
        msgPop({msg: '参数值必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.devicePasModify.method,
        url: apis.devicePasModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//删除
function pas_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.devicePasDel.method,
            url: apis.devicePasDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//列表
function getIndex(){
    var formData = {
        start: pages.start,
        limit: pages.limit
    };
    $.ajax({
        type: apis.deviceIndex.method,
        url: apis.deviceIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            formList = res.data;
            $('#pageTotal').html(res.total);
            indexHtml();
            var pageObj = pages; pageObj.total = res.total;
            pageHtml(pageObj);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}
//列表字符串
function indexHtml(){
    if (formList.length == 0) {
        msgPop({msg: '没有数据'}); return
    }
    var d = formList;
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].com_name + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].cate_name + '</td>';
        h += '<td>';
        if (d[i].thumb == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].thumb + '" height="50"/>';
        }
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>' + d[i].sale + '</td>';
        h += '<td class="f-14">';
        h += '<a title="编辑" href="javascript:;" onclick="device_edit(\'编辑\',\'device-edit.html\', ' + d[i].id + ')" style="text-decoration:none">';
        h += '修改';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        //h += '<a href="javascript:;" onclick="list_show(\'查看详情\',\'case-show.html\', ' + d[i].id + ');">';
        //h += '详情';
        //h += '<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//列表
function getOption(){
    $.ajax({
        type: apis.deviceOption.method,
        url: apis.deviceOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var cate = res.data.cate;
            var h = '', i;
            h = '<option value="">';
            h += '-所有-';
            h += '</option>';
            for (i = 0; i < cate.length; i ++) {
                h += '<option value="' + cate[i].id + '">';
                h += cate[i].title;
                h += '</option>';
            }
            $('#cateid').html(h);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//编辑
function device_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//显示主图
function showImg(e){
    console.log(e);
    var h = '';
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).width(e.w).height(e.h);
}


//更新
function modify(){
    var img = $('#img').val();
    img = img ? img : '';
    var formData = {
        id: id,
        cname: $('#cname').val(),
        title: $('#title').val(),
        cateid: $('#cateid').val(),
        img: img,
        price: $('#price').val(),
        num: $('#num').val(),
        sale: $('#sale').val(),
    };
    console.log(formData); return
    $.ajax({
        type: apis.caseOne.method,
        url: apis.caseOne.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var thumb = '';
            $('#com_name').html(d.com_name);
            $('#buyer_name').html(d.buyer_name);
            $('#title').html(d.title);
            $('#cate_name').html(d.cate_name);
            if (d.thumb != '') {
                $('#thumb').html('<img src="' + d.thumb + '" style="max-width: 500px;"/>');
            }
            $('#address').html(d.address);
            $('#designer_name').html(d.designer_name);
            $('#item_area').html(d.item_area + '平方');
            $('#main_material').html(d.main_material);
            $('#item_detail').html(d.item_detail);
            $('#item_finish_time').html(d.item_finish_time);
            $('#like_num').html(d.like_num);
            $('#collect_num').html(d.collect_num);
            $('#forward_num').html(d.forward_num);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//列表
function getOne(){
    $.ajax({
        type: apis.caseOne.method,
        url: apis.caseOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var thumb = '';
            $('#com_name').html(d.com_name);
            $('#buyer_name').html(d.buyer_name);
            $('#title').html(d.title);
            $('#cate_name').html(d.cate_name);
            if (d.thumb != '') {
                $('#thumb').html('<img src="' + d.thumb + '" style="max-width: 500px;"/>');
            }
            $('#address').html(d.address);
            $('#designer_name').html(d.designer_name);
            $('#item_area').html(d.item_area + '平方');
            $('#main_material').html(d.main_material);
            $('#item_detail').html(d.item_detail);
            $('#item_finish_time').html(d.item_finish_time);
            $('#like_num').html(d.like_num);
            $('#collect_num').html(d.collect_num);
            $('#forward_num').html(d.forward_num);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}